package br.net.woodstock.tickettoride.web.jsf;

import br.net.woodstock.rockframework.utils.ConditionUtils;
import br.net.woodstock.rockframework.web.faces.ManagedBean;
import br.net.woodstock.rockframework.web.faces.utils.FacesUtils;
import br.net.woodstock.tickettoride.orm.AbstractIntegerEntity;
import br.net.woodstock.tickettoride.web.jsf.security.common.JSFConstants;

public abstract class TTRBean extends ManagedBean {

	private static final long		serialVersionUID	= -4575594819057334506L;

	protected static final String	INDEX_VIEW			= JSFConstants.INDEX_PAGE;

	public static final String		MSG_BUNDLE			= "messages";

	public static final String		MSG_OPERATION_OK	= "msg.operation.ok";

	public static final String		MSG_OPERATION_ERROR	= "msg.operation.error";

	public TTRBean() {
		super();
	}

	protected boolean isEmptyId(final AbstractIntegerEntity e) {
		if (e != null) {
			Integer id = e.getId();
			return !this.isValidId(id);
		}
		return false;
	}

	protected boolean isValidId(final Integer id) {
		if (id != null) {
			if (id.intValue() != 0) {
				return true;
			}
		}
		return false;
	}

	protected boolean isValidId(final String id) {
		if (ConditionUtils.isNotEmpty(id)) {
			try {
				Integer i = Integer.valueOf(id);
				return this.isValidId(i);
			} catch (NumberFormatException e) {
				return false;
			}
		}
		return false;
	}

	protected void onStart() {
		//
	}

	protected void onEnd() {
		//
	}

	protected String getMessage(final String key) {
		return FacesUtils.getMessage(TTRBean.MSG_BUNDLE, key);
	}

	protected String getMessageOperationOK() {
		return FacesUtils.getMessage(TTRBean.MSG_BUNDLE, TTRBean.MSG_OPERATION_OK);
	}

	protected String getMessageOperationError() {
		return FacesUtils.getMessage(TTRBean.MSG_BUNDLE, TTRBean.MSG_OPERATION_ERROR);
	}

}
